package com.logic.landseaserver.ws;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.ActivityList;
import com.logic.landseaserver.domain.ActivityListDetail;
import com.logic.landseaserver.domain.ActivityListDetailDynamic;
import com.logic.landseaserver.persistence.read.ActivityListDetailReadMapper;
import com.logic.landseaserver.persistence.write.ActivityListDetailWriteMapper;
import com.logic.landseaserver.service.IActivityService;
import com.logic.landseaserver.service.IPubActivityService;
import com.logic.landseaserver.ws.dto.ActivityAndActivityListDTO;
import com.logic.landseaserver.ws.dto.ActivityDTO;
import com.logic.landseaserver.ws.dto.ActivityDynamicReq;
import com.logic.landseaserver.ws.dto.ActivityListDTO;
import com.logic.landseaserver.ws.dto.ActivityListDTO1;
import com.logic.landseaserver.ws.dto.ActivityListDetailDTO;
import com.logic.landseaserver.ws.dto.ActivityListDetailDynamicDTO;
import com.logic.landseaserver.ws.dto.PubActivityDTO;
import com.logic.landseaserver.ws.dto.SaveActivityListDetailDynamicReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Author: jack
 * @Date: 2017/5/4
 * @Description: 活动控制类
 */
@Path("/activity")
@Api(value = "activity")
public class ActivityRest
{
    
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private IActivityService iactivityServiceImpl;
    
    @Autowired
    private IPubActivityService ipubActivityServiceImpl;
    
    @Autowired
    private ActivityListDetailReadMapper activityListDetailReadMapper;
    
    @Autowired
    private ActivityListDetailWriteMapper activityListDetailWriteMapper;
    
    // 根据类型进行查询
    @POST
    @Path("/getActivityForFlag")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "项目列表，网站和微信端   参数格式{flag:xxx,area:xxx}", position = 4)
    public Response getActivityForFlag(@ApiParam(value = "param", required = true) Map pmap)
    {
        CommonResultMap map = new CommonResultMap();
        Map<String, Object> result = new HashMap<String, Object>();
        try
        {
            List<PubActivityDTO> projects = ipubActivityServiceImpl.getActivityListForFlag((HashMap)pmap);
            
            result.put("projects", projects);
            result.put("total", projects.size());
            map.setData(result);
        }
        catch (Exception e)
        {
            logger.error("getAcctivityForFlag error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 增加
    @POST
    @Path("/createActivity")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Create a new activity", position = 1)
    public Response createActivity(@ApiParam(value = "param", required = true) ActivityDTO activityDTO)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            int id = iactivityServiceImpl.insertActivity(activityDTO);
            activityDTO.setId(id);
            map.setData(activityDTO);
            map.setResult(CommonResult.SUCCESS, "Create success");
        }
        catch (BusinessException e)
        {
            logger.error("createActivity error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("createActivity error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/createActivityList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Create a new createActivityList", position = 1)
    public Response createActivityList(@ApiParam(value = "param", required = true) List<ActivityList> activityLists)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            for (ActivityList activityList : activityLists)
            {
                int activityListId = iactivityServiceImpl.insertActivityList(activityList);
                activityList.setId(activityListId);
            }
            map.setData(activityLists);
            map.setResult(CommonResult.SUCCESS, "Create success");
        }
        catch (Exception e)
        {
            logger.error("createActivity error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/createActivityListDeatil")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Create a new createActivityListDeatil", position = 1)
    public Response createActivityListDeatil(
        @ApiParam(value = "param", required = true) ActivityListDetail activityListDetail)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            
            int id = iactivityServiceImpl.insertActivityListDetail(activityListDetail);
            activityListDetail.setId(id);
            map.setData(activityListDetail);
            map.setResult(CommonResult.SUCCESS, "Create success");
        }
        catch (Exception e)
        {
            logger.error("createActivity error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/createActivityListDetailDynamic")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Create a new createActivityListDetailDynamic", position = 1)
    public Response createActivityListDetailDynamic(
        @ApiParam(value = "param", required = true) List<ActivityDynamicReq> activityListDetailDynamics)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iactivityServiceImpl.insertActivityListDetailDynamic(activityListDetailDynamics);
            map.setData(activityListDetailDynamics);
            map.setResult(CommonResult.SUCCESS, "Create success");
        }
        catch (Exception e)
        {
            logger.error("createActivity error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 查询活动列表
    @POST
    @Path("/activityList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "list Activity", position = 2)
    public Response activityList(@ApiParam(value = "param", required = true) @QueryParam("activityId") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        List<ActivityListDTO1> activityList = null;
        try
        {
            activityList = iactivityServiceImpl.queryActivityListDTO(id);
            map.setData(activityList);
            map.setResult(CommonResult.SUCCESS);
            
        }
        catch (Exception e)
        {
            logger.error("list error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 查询活动列表详情
    @GET
    @Path("/activityListDetail/{activityListId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "query activityListDetail", position = 2)
    public Response activityListDetail(
        @ApiParam(value = "param", required = true) @PathParam("activityListId") Integer activityListId)
    {
        CommonResultMap map = new CommonResultMap();
        ActivityListDetailDTO activityListDetail = null;
        try
        {
            activityListDetail = iactivityServiceImpl.queryActivityListDetailDTO(activityListId);
            map.setData(activityListDetail);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            logger.error("list error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 查询活动列表详情动态
    @GET
    @Path("/activityListDetailDynamic/{activityListDetailId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "query activityListDetailDynamic", position = 2)
    public Response activityListDetailDynamic(
        @ApiParam(value = "param", required = true) @PathParam("activityListDetailId") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        List<ActivityListDetailDynamicDTO> activityListDetailDynamic = null;
        try
        {
            activityListDetailDynamic = iactivityServiceImpl.queryActivityListDetailDynamicDTO(id);
            map.setData(activityListDetailDynamic);
            map.setResult(CommonResult.SUCCESS);
            
        }
        catch (Exception e)
        {
            logger.error("list error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 查询所有信息
    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "list Activity", position = 2)
    public Response list(@ApiParam(value = "param", required = true) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<ActivityDTO> activityDTOList = null;
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            activityDTOList = (List<ActivityDTO>)iactivityServiceImpl.query(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(activityDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
            
        }
        catch (Exception e)
        {
            logger.error("list error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 从最外层删除
    @DELETE
    @Path("/delete/activity")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete activity by set deleted", position = 3)
    public Response logicActivityDelete(@ApiParam(value = "param", required = true) @QueryParam("id") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iactivityServiceImpl.deleteActivity(id);
            List<ActivityList> activityLists = iactivityServiceImpl.queryActivityList(id);
            iactivityServiceImpl.deleteActivityList(id);
            for (ActivityList activityList : activityLists)
            {
                ActivityListDetail activityListDetail =
                    iactivityServiceImpl.queryActivityListDetail(activityList.getId());
                iactivityServiceImpl.deleteActivityListDetail(activityList.getId());
                iactivityServiceImpl.deleteActivityListDetailDynamic(activityListDetail.getId());
            }
            map.setResult(CommonResult.SUCCESS, "delete activity");
        }
        catch (Exception e)
        {
            logger.error("logicDelete error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 先删详情，删动态，再删列表
    @DELETE
    @Path("/delete/activityListDetailFirst")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete activityListDetailFirst by set deleted", position = 3)
    public Response logicAtivityListDetailFirstDelete(
        @ApiParam(value = "param", required = true) @QueryParam("detailId") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            ActivityListDetail activityListDetail = activityListDetailReadMapper.selectByPrimaryKey(id);
            iactivityServiceImpl.deleteActivityListDetailByPrimaryKey(id);
            iactivityServiceImpl.deleteActivityListDetailDynamic(id);
            iactivityServiceImpl.deleteActivityListByDetailId(activityListDetail.getActivityListId());
            map.setResult(CommonResult.SUCCESS, "delete activityListDetail");
        }
        catch (Exception e)
        {
            logger.error("logicDelete error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 删动态
    @DELETE
    @Path("/delete/activityListDetailDynamic")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete activityListDetailDynamic by set deleted", position = 3)
    public Response logicAtivityListDetailDynamicDelete(
        @ApiParam(value = "param", required = true) @QueryParam("id") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iactivityServiceImpl.deleteActivityListDetailDynamicByPrimaryKey(id);
            map.setResult(CommonResult.SUCCESS, "delete activityListDetailDynamic");
        }
        catch (Exception e)
        {
            logger.error("logicDelete error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 编辑
    @PUT
    @Path("/editActivity")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Edit Activity ")
    public Response updateOrder(@ApiParam(value = "param", required = true) ActivityDTO activityDTO)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            logger.info("editActivity|" + activityDTO);
            iactivityServiceImpl.editActivity(activityDTO);
            map.setResult(CommonResult.SUCCESS, "edit activity");
        }
        catch (Exception e)
        {
            logger.error("updateOrder error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 编辑
    @PUT
    @Path("/editActivityList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Edit ActivityList")
    public Response editActivityList(@ApiParam(value = "param", required = true) List<ActivityList> activityList)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iactivityServiceImpl.editActivityList(activityList);
            map.setResult(CommonResult.SUCCESS, "edit activityList");
        }
        catch (Exception e)
        {
            logger.error("updateOrder error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 编辑
    @PUT
    @Path("/editActivityListDetail")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Edit activityListDetail")
    public Response editActivityListDetail(
        @ApiParam(value = "param", required = true) ActivityListDetail activityListDetail)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iactivityServiceImpl.editActivityListDetail(activityListDetail);
            map.setResult(CommonResult.SUCCESS, "edit activityListDetail");
        }
        catch (Exception e)
        {
            logger.error("updateOrder error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @PUT
    @Path("/editActivityListDetailDynamic")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Edit activityListDetailDynamic")
    public Response editActivityListDetailDynamic(
        @ApiParam(value = "param", required = true) ActivityListDetailDynamic activityListDetailDynamic)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iactivityServiceImpl.editActivityListDetailDynamic(activityListDetailDynamic);
            map.setResult(CommonResult.SUCCESS, "edit activityListDetailDynamic");
        }
        catch (Exception e)
        {
            logger.error("updateOrder error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 显示
    @POST
    @Path("/getActivity")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取门店跟品牌活动", position = 4)
    public Response getActivity()
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            List<ActivityListDTO> dtos = iactivityServiceImpl.queryList();
            map.setData(dtos);
        }
        catch (Exception e)
        {
            logger.error("getAcctivityForFlag error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 发布
    @PUT
    @Path("/publishedActivity")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "published Activity  参数格式{id:xxx,flag:xxx}")
    public Response publishedActivity(@ApiParam(value = "param", required = true) Map pmap)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Integer a = iactivityServiceImpl.publishedActivity(pmap);
            map.setData(a);
            map.setResult(CommonResult.SUCCESS, "published activity");
        }
        catch (Exception e)
        {
            logger.error("updateOrder error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    // 取消发布
    @PUT
    @Path("/canclePublishedActivity")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "cancle published Activity ")
    public Response canclePublishedActivity(@ApiParam(value = "param", required = true) @QueryParam("id") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iactivityServiceImpl.canclePublishedActivity(id);
            map.setResult(CommonResult.SUCCESS, "cancle published activity");
        }
        catch (Exception e)
        {
            logger.error("updateOrder error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/editActivityAndActivityListDTO")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Edit ActivityAndActivityListDTO")
    public Response editActivityAndActivityListDTO(
        @ApiParam(value = "param", required = true) ActivityAndActivityListDTO dto)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iactivityServiceImpl.editActivityAndActivityListDTO(dto);
            map.setData(dto);
            map.setResult(CommonResult.SUCCESS, "edit ActivityAndActivityListDTO");
        }
        catch (Exception e)
        {
            logger.error("updateOrder error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    /**
     * 
     * [简要描述]：保存</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-08-24)</br>
     *
     * @param activityListDetail
     * @return
     *
     */
    @POST
    @Path("/saveActivityListDetail")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "保存活动详情", position = 1)
    public Response saveActivityListDetail(
        @ApiParam(value = "param", required = true) ActivityListDetail activityListDetail)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Integer acDetailId = activityListDetail.getId();
            if (null == acDetailId)
            {
                int id = iactivityServiceImpl.insertActivityListDetail(activityListDetail);
                activityListDetail.setId(id);
                map.setData(activityListDetail);
                map.setResult(CommonResult.SUCCESS, "Create success");
            }
            else
            {
                iactivityServiceImpl.editActivityListDetail(activityListDetail);
                map.setResult(CommonResult.SUCCESS, "edit activityListDetail");
            }
            
        }
        catch (Exception e)
        {
            logger.error("createActivity error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/saveActivityListDetailDynamic")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "保存动态活动详情", position = 1)
    public Response saveActivityListDetailDynamic(
        @ApiParam(value = "param", required = true) SaveActivityListDetailDynamicReq saveActivityListDetailDynamicReq)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Integer acDetailId = saveActivityListDetailDynamicReq.getId();
            if (null == acDetailId)
            {
                iactivityServiceImpl.saveDynamicAcInfoInsert(saveActivityListDetailDynamicReq);
                map.setData(saveActivityListDetailDynamicReq);
                map.setResult(CommonResult.SUCCESS, "Create success");
            }
            else
            {
                iactivityServiceImpl.saveDynamicAcInfoEdit(saveActivityListDetailDynamicReq);
                map.setResult(CommonResult.SUCCESS, "edit activityListDetail");
            }
        }
        catch (Exception e)
        {
            logger.error("createActivity error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
